<template>
  <div>
    <el-table
    :tree-props="tp"
    class="table"
      :row-key="rowkey"
      :data="data"
      label-width="100px"
      border
      style="width: 100%"
      v-loading="loading"
    > 
      <el-table-column
      :label="col.title"
        :prop="col.name"
        :type="col.type"
        :width="col.width"
        :align="col.align"
        v-for="(col, index) in cols"
        :key="index"
      >
        <template v-if="col.slot" v-slot="{ row, inedx }">
          <slot :row="row" :index="index" :name="col.slot"> </slot>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="block">
      <el-pagination
      v-if="value"
        @size-change="(v) => change('pagesize',v)"
        @current-change="(v) => change('pagenum',v)"
        :current-page="value.pagenum"
        :page-sizes="[5, 10, 15, 30]"
        :page-size="value.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: "MyTable",
  props: {

    /**
     * 当有children属性
     */
    tp:{
      type:Object,
       default: () => ({ hasChildren: 'hasChildren', children: 'children' } ),
    },
    loading: Boolean,
    /**
     * 列
     */
    cols: {
      type: Array,
      required: true,
      default: () => [],
    },
    /**
     * 分页对象(传入则 开启分页 )
     */
    value: Object,
    /**
     * 总条数
     */
    total: Number,
    /**
     * 数据
     */
    data: {
      required: true,
      type: Array,
      default: () => [],
    },
    /**
     * rowKey
     */
    rowkey: {
      type: String,
      default: "id",
    },
  },

  mounted() {},
  methods: {
    /**
     * 分页活页码更改事件
     */
    change(key ,value){
  const obj={...this.value, [key]: value};
          this.$emit('input',obj);
          this.$emit('change',obj);
    }
  },
};
</script>

<style lang="scss" scoped>
.table{
 margin-bottom: 15px; 
}
</style>
